package logic

import (
	"cloud-disk/core/model"
	"cloud-disk/xorm"
	"context"

	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserFileMoveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserFileMoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFileMoveLogic {
	return &UserFileMoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserFileMoveLogic) UserFileMove(req *types.UserFileMoveRequest, userIdentity string) (resp *types.UserFileMoveResponse, err error) {
	parentData := new(model.UserRepostiory)
	resp = new(types.UserFileMoveResponse)
	has, err := xorm.Xorm.Where("identity = ? and user_identity = ?", req.ParentIdentity, userIdentity).Get(parentData)
	if err != nil {
		resp.Message = "修改失败 " + err.Error()
		resp.Code = 100
		return resp, nil
	}

	if !has {
		resp.Message = "文件夹不存在"
		resp.Code = 100
		return resp, nil
	}

	err = l.svcCtx.UserRepostioryModel.UpdateFileParentIdByIdentity(req.Identity, userIdentity, int(parentData.Id))
	if err != nil {
		resp.Message = "移动失败 " + err.Error()
		resp.Code = 100
		return resp, nil
	}
	resp.Message = "移动成功"
	resp.Code = 200
	return
}
